//
//  CinemaViewController.m
//  MaxMovie
//
//  Created by Maxdong on 14-12-24.
//  Copyright (c) 2014年 lanou3g.com 蓝欧科技. All rights reserved.
//

#import "CinemaViewController.h"
#import "NewCell.h"
#import "NetworkHandler.h"
#import "UIImageView+WebCache.h"
#import "NewModel.h"
#import "NewsNRViewController.h"
#import "VideoPlayerViewController.h"
#import "HeaderViewController.h"
@interface CinemaViewController ()<UIWebViewDelegate, UITableViewDataSource, UITableViewDelegate>


// models
@property(nonatomic, retain)NewModel *New;
@property(nonatomic, retain)NSArray *myArr;
@property(nonatomic, retain)NSArray *myArr2;

// views
@property(nonatomic, retain)UITableView *myTableView;
@property(nonatomic, retain)UIImageView *myImageView;
@property(nonatomic, retain)UIButton *BigButton;
@property(nonatomic, retain)UIButton *bofangButton;

@end

@implementation CinemaViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        self.New = [[NewModel alloc]init];
        self.array = [NSMutableArray array];
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    
    [self create];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor purpleColor];
    
    
    NSString * str2 =@"http://api.m.mtime.cn/News/Detail.api?newsId=1535626";
    [NetworkHandler connectioonNetGETDataWithURLString:str2 completion:^(id result) {
        
        
    self.strrr =[result  objectForKey:@"wapUrl"];

     }];
    
    
    NSString *str1 = @"http://api.m.mtime.cn/PageSubArea/GetRecommendationIndexInfo.api";
    [NetworkHandler connectioonNetGETDataWithURLString:str1 completion:^(id result) {
        
        //大图 图片
        NSString *headerViewImageStr = [[result valueForKey:@"news"] valueForKey:@"imageUrl"];
        UIImageView *tmpImageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0,self.view.frame.size.width, 210)];
        [tmpImageView setUserInteractionEnabled:YES];
        [tmpImageView setImageWithURL:[NSURL URLWithString:headerViewImageStr]];
        [self.myTableView setTableHeaderView:tmpImageView];
        [tmpImageView release];
        
        //点击按钮
        UIButton *HeaderButton = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 210)];
        [HeaderButton addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
        [HeaderButton setTag:[[[result valueForKey:@"news"] valueForKey:@"newsID"] integerValue]];
        [tmpImageView addSubview:HeaderButton];
        [HeaderButton release];
        self.bofangButton = HeaderButton;
        
        //大图上得标题
        NSString *headerViewTitleLabelStr = [[result valueForKey:@"news"] valueForKey:@"title"];
        UILabel *headerViewTitleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 180, self.view.frame.size.width, 30)];
        [headerViewTitleLabel setText:headerViewTitleLabelStr];
        NSLog(@"%@", headerViewTitleLabelStr);
        [headerViewTitleLabel setBackgroundColor:[UIColor colorWithRed:128.0 / 255.0 green:0.0 / 255.0 blue:128.0 / 255.0 alpha:1.0]];
        headerViewTitleLabel.font = [UIFont systemFontOfSize:14];
        [headerViewTitleLabel setTextAlignment:NSTextAlignmentCenter];
        [headerViewTitleLabel setTextColor:[UIColor colorWithRed:255.0 / 255.0 green:215.0 / 255.0 blue:0.0 / 255.0 alpha:1.0]];
        [headerViewTitleLabel setAlpha:0.7];
        [tmpImageView addSubview:headerViewTitleLabel];
        
    }];
    
    
    
    NSString *str = @"http://api.m.mtime.cn/News/NewsList.api?pageIndex=1";
    [NetworkHandler connectioonNetGETDataWithURLString:str completion:^(id result) {
        self.myArr = [result objectForKey:@"newsList"];
        [self.myTableView reloadData];
    }];
    
    
    
    UITableView *tmpTableView = [[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStylePlain];
    tmpTableView.dataSource = self;
    tmpTableView.delegate = self;
    [tmpTableView registerClass:[NewCell class] forCellReuseIdentifier:@"reuse"];
    [self.view addSubview:tmpTableView];
    [tmpTableView release];
    self.myTableView = tmpTableView;
    
    //刷新
  //  [self setupRefresh];
}

///**
// *  集成刷新控件
// */
//- (void)setupRefresh
//{
//    // 1.下拉刷新(进入刷新状态就会调用self的headerRereshing)
//    [self.myTableView addHeaderWithTarget:self action:@selector(headerRereshing)];
//    //#warning 自动刷新(一进入程序就下拉刷新)
//    [self.myTableView headerBeginRefreshing];
//    [self.myTableView reloadData];
//    
//    // 2.上拉加载更多(进入刷新状态就会调用self的footerRereshing)
//    [self.myTableView addFooterWithTarget:self action:@selector(footerRereshing)];
//}
//
//#pragma mark 开始进入刷新状态
//- (void)headerRereshing
//{
//    //    // 1.添加假数据
//    //    for (int i = 0; i<5; i++) {
//    //        [self.fakeData insertObject:MJRandomData atIndex:0];
//    //    }
//    
//    // 2.2秒后刷新表格UI
//    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
//        // 刷新表格
//        [self.myTableView reloadData];
//        
//        // (最好在刷新表格后调用)调用endRefreshing可以结束刷新状态
//        [self.myTableView headerEndRefreshing];
//    });
//}
//
//- (void)footerRereshing
//{
//    
//    
//    //    // 1.添加假数据
//    //    for (int i = 0; i< 10; i++) {
//    //
//    //        NSString *MJRandomData = [NSString stringWithFormat:@"http://api.m.mtime.cn/News/NewsList.api?pageIndex=%d",i];
//    //       [self.fakeData addObject:MJRandomData];
//    //    }
//    
//    //    // 1.添加假数据
//    //    for (int i = 0; i<5; i++) {
//    //        [self.fakeData addObject:MJRandomData];
//    //    }
//    
//    // 2.2秒后刷新表格UI
//    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
//        // 刷新表格
//        [self.myTableView reloadData];
//        
//        // (最好在刷新表格后调用)调用endRefreshing可以结束刷新状态
//        [self.myTableView footerEndRefreshing];
//    });
//}



-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
   return 70;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (self.myArr.count > 0)
    {
        return self.myArr.count;
        
    }
    return 1;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *str = @"reuse";
    NewCell *cell = [tableView dequeueReusableCellWithIdentifier:str];
    if (cell) {
        cell = [[[NewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:str] autorelease];
    }
    //kvc
    NSDictionary *dic = [self.myArr objectAtIndex:indexPath.row];
    [self.New setValuesForKeysWithDictionary:dic];
    
    //图片
    [cell.myImageView setImageWithURL:[NSURL URLWithString:self.New.image]];
    
    //标题
    [cell.titleLabel setText:self.New.title];
    
    //内容
    [cell.sumLabel setText:self.New.summaryInfo];
    
    return cell;
}

//获取每一行cell的ID
- (void)create
{
    NSString *str = @"http://api.m.mtime.cn/News/NewsList.api?pageIndex=1";
    NSURL *url = [NSURL URLWithString:str];
    NSData *data = [NSData dataWithContentsOfURL:url];
    NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSUTF8StringEncoding error:nil];
    NSArray *array = [dic objectForKey:@"newsList"];
    
    for (NSDictionary *myDic in array) {
        [_array addObject:[myDic objectForKey:@"id"]];
        
    }
    
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
   // NSString *strID = [[self.myArr objectAtIndex:indexPath.row]valueForKey:@"id"];
    NewsNRViewController *NewsNRVC = [[NewsNRViewController alloc]init];
    NewsNRVC.titleStr = self.New.title;
    
    
    NewsNRVC.MyidStr =[NSString stringWithFormat:@"http://api.m.mtime.cn/News/Detail.api?newsId=%@",[_array objectAtIndex:indexPath.row]];
    //NSLog(@"mystr = %@",NewsNRVC.MyidStr);
    NewsNRVC.summaryInfoStr = self.New.summaryInfo;
    NewsNRVC.imageStr = self.New.image;
    
    [self.navigationController pushViewController:NewsNRVC animated:YES];
    
}



-(void)buttonClicked:(UIButton *)button
{
    
    HeaderViewController *header= [[HeaderViewController alloc]init];
    
    header.str = _strrr;
    
    [self.navigationController pushViewController:header animated:YES];
    
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
  
}



@end
